﻿/*
 * Created by SharpDevelop.
 * User: Marian
 * Date: 19.03.2015
 * Time: 13:51
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
 
using System;
using System.IO;
using MimeKit;

namespace PM.Net {
    using PM.Net.DeltaSync;
    using PM.Net.DeltaSync.hu01;
    
    class Program {
    	#if DEBUG
    	static Program() {
			DeltaSyncClient.OnHU01Exception = DumpHU01Contents;
    	}
        public static void DumpHU01Contents(string messageId, HU01Exception ex, Stream hu01stream) {
            System.Diagnostics.Debug.WriteLine("HU01 decompression failed: " + ex.Message);
            System.Diagnostics.Debug.WriteLine("Dumping HU01 stream as BASE64 for message " + messageId);
            System.Diagnostics.Debug.WriteLine("Please submit the BASE64 encoded message content");
            System.Diagnostics.Debug.WriteLine("and the plain text message content to the JDeltaSync");
            System.Diagnostics.Debug.WriteLine("issue tracker. The plain text message content can");
            System.Diagnostics.Debug.WriteLine("be retrieved by clicking \"View message source\" in");
            System.Diagnostics.Debug.WriteLine("the Hotmail web UI.");
            
            using(var mem = new MemoryStream()) {
            	try {
            		hu01stream.CopyTo(mem);
                	mem.Position = 0;
                	
                	PM.Debug.DumpData.DumpStream(mem, ".hu01");
                	mem.Position = 0;
                	
                	using (var dumpstm = PM.Debug.DumpData.CreateDumpStream(".base64")) {
                		var dumpdata = System.Text.Encoding.ASCII.GetBytes(Convert.ToBase64String(mem.ToArray(), Base64FormattingOptions.InsertLineBreaks));
                		dumpstm.Write(dumpdata, 0, dumpdata.Length);
                	}
            	} catch (Exception ex2) {
                	System.Diagnostics.Debug.WriteLine("Failed to dump HU01 stream: " + ex2.Message);
            	}
            }
    	}
        #endif
        
    	public static void test1() {
    		DeltaSyncClientHelper client = new DeltaSyncClientHelper();
            client.Login("yourname@hotmail.com", "yourpassword");
            try {
                Message[] messages = client.GetMessages(client.Inbox);
                Console.WriteLine(string.Format("{0} messages in Inbox", messages.Length));
                foreach (Message message in messages) {
                    FileInfo file = new FileInfo(message.Id + ".eml");
                    Console.WriteLine();
                    Console.WriteLine("Downloading message from \"" 
                        + message.From    + "\" with subject \"" 
                        + message.Subject + "\" received at " 
                        + message.DateReceived);
                    using (Stream output = file.Create()) {
                        //client.DownloadMessageContent(message, output);
                    
                        var emailmessage = client.DownloadMessage(message, (Stream stream) => new MimeParser(stream).ParseMessage());
                        emailmessage.WriteTo(FormatOptions.Default, output);
                    }
                }
            } finally {
                client.Close();
            }
        }
        
        public static void Main(string[] args) {
            try {
                test1();
            } catch (AggregateException ex) {
    			Console.WriteLine();
    			Console.WriteLine(string.Format("{0} failure: {1}\n{2}", ex.GetType().FullName, ex.Message, ex.StackTrace));
                foreach (Exception tmp in ex.InnerExceptions)
                    Console.WriteLine(string.Format(">>> {0} failure: {1}\n{2}", tmp.GetType().FullName, tmp.Message, tmp.StackTrace));
            } catch (Exception ex) {
    			Console.WriteLine();
                Console.WriteLine(string.Format("{0} failure: {1}\n{2}", ex.GetType().FullName, ex.Message, ex.StackTrace));
                for (Exception tmp = ex.InnerException; tmp != null; tmp = tmp.InnerException) {
                    Console.WriteLine(string.Format(">>> {0} failure: {1}\n{2}", tmp.GetType().FullName, tmp.Message, tmp.StackTrace));
                }
                Environment.ExitCode = 1;
            }
        }
    }
}